package com.sise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.bean.CodeMsg;
import com.sise.entity.ProductType;
import com.sise.entity.StoreInfo;
import com.sise.mapper.ProductTypeMapper;
import com.sise.mapper.StoreInfoMapper;
import com.sise.service.IStoreInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zj
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements IStoreInfoService {

    @Resource
    private StoreInfoMapper storeInfoMapper;

    @Resource
    private ProductTypeMapper productTypeMapper;

    @Resource
    private IStoreInfoService storeInfoService;

    @Override
    public boolean save(StoreInfo storeInfo) {
        int tag = storeInfoMapper.insert(storeInfo);
        return tag == 1;
    }

    @Override
    public boolean deleteById(Integer storeId) {
        int tag = storeInfoMapper.deleteById(storeId);
        return tag == 1;
    }

    @Override
    public boolean deleteByIdList(List<Integer> storeIdList) {
        int tag = storeInfoMapper.deleteBatchIds(storeIdList);
        return tag > 0;
    }

    @Override
    public boolean updateById(StoreInfo storeInfo) {
        int tag = storeInfoMapper.updateById(storeInfo);
        return tag == 1;
    }

    @Override
    public StoreInfo findById(Integer storeId) {
        return storeInfoMapper.selectById(storeId);
    }

    @Override
    public List<StoreInfo> findAll() {
        QueryWrapper<StoreInfo> storeInfoQueryWrapper = new QueryWrapper<>();
        storeInfoQueryWrapper.lambda().eq(StoreInfo::getStoreStatus, "营业中");
        return storeInfoMapper.selectList(storeInfoQueryWrapper);
    }

    @Transactional
    @Override
    public CodeMsg<String> addStoreInfo(StoreInfo storeInfo, ProductType productType) {
        storeInfoMapper.insert(storeInfo.setStorePraiseRate(0f));
        if (productType == null || "".equals(productType.getProductTypeName())){
            return new CodeMsg<String>().setCode(400).setMsg("商品类型为必填");
        }
        productTypeMapper.insert(productType.setStoreId(storeInfo.getStoreId()));
        return new CodeMsg<String>().setCode(200).setMsg("添加成功");
    }

    @Override
    public CodeMsg<String> updateStoreInfo(StoreInfo storeInfo) {
        int tag = storeInfoMapper.updateById(storeInfo);
        if (tag == 1) {
            return new CodeMsg<String>().setCode(200).setMsg("更新成功");
        }
        return new CodeMsg<String>().setCode(400).setMsg("更新失败");
    }

    @Override
    public CodeMsg<List<StoreInfo>> getShopInfo() {
        return new CodeMsg<List<StoreInfo>>().setCode(200).setMsg("获取成功").setData(storeInfoMapper.selectList(null));
    }

    @Override
    public CodeMsg<String> updateStoreInfoByRest(int[] storeIds) {
        List<StoreInfo> bookList = new ArrayList<>();
        for (int id : storeIds) {
            bookList.add(new StoreInfo().setStoreId(id).setStoreStatus("休息中"));
        }
        if (storeInfoService.updateBatchById(bookList)) {
            return new CodeMsg<String>().setCode(200).setMsg("操作成功");
        }
        return new CodeMsg<String>().setCode(400).setMsg("操作失败");
    }

    @Override
    public CodeMsg<String> updateStoreInfoByBusiness(int[] storeIds) {
        List<StoreInfo> bookList = new ArrayList<>();
        for (int id : storeIds) {
            bookList.add(new StoreInfo().setStoreId(id).setStoreStatus("营业中"));
        }
        if (storeInfoService.updateBatchById(bookList)) {
            return new CodeMsg<String>().setCode(200).setMsg("操作成功");
        }
        return new CodeMsg<String>().setCode(400).setMsg("操作失败");
    }
}
